package com.dwy2002.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.dwy2002.common.Result;
import com.dwy2002.common.exception.EmptyIdException;
import com.dwy2002.common.utils.JwtUtil;
import com.dwy2002.enums.Status;
import com.dwy2002.pojo.dto.users.ProductFavoritesPageDTO;
import com.dwy2002.pojo.po.product.Products;
import com.dwy2002.pojo.po.users.ProductFavorites;
import com.dwy2002.pojo.po.users.Users;
import com.dwy2002.product.mapper.ProductFavoritesMapper;
import com.dwy2002.product.service.IProductFavoritesService;
import com.dwy2002.product.service.IProductsService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dwy2002.common.Result.*;
import static com.dwy2002.common.utils.Utils.*;
import static com.dwy2002.enums.rabbit.FavoritesRabbitConstants.FAVORITES_REQUEST_QUEUE;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/17
 */
@Service
@RequiredArgsConstructor
public class ProductFavoritesServiceImpl extends ServiceImpl<ProductFavoritesMapper, ProductFavorites> implements IProductFavoritesService {
    private final JwtUtil jwtUtil;
    private final IProductsService productsService;

    /**
     * 返回用户全部商品收藏
     *
     * @param dto 商品收藏DTO
     * @author 杜伟毅
     * @since 2025/3/17 0017 23:33
     */
    @Override
    public Result<List<Products>> getProductFavorites(ProductFavoritesPageDTO dto) {
        String userId = jwtUtil.parseToken(getToken(), Users.class).getId();

        List<String> productIds = list(
                new LambdaQueryWrapper<ProductFavorites>()
                        .eq(ProductFavorites::getUserId, userId)
                        .select(ProductFavorites::getProductId))
                .stream()
                .map(ProductFavorites::getProductId)
                .toList();

        if (isListEmpty(productIds))
            return fail(Status.NOT_FOUND, "你当前并没有收藏商品，快去收藏吧");


        return success(productsService.queryProductsWithDynamicConditions(productIds, dto));
    }

    /**
     * 收藏商品
     *
     * @param productId 商品id
     * @author 杜伟毅
     * @since 2025/3/18 0018 14:07
     */
    @Override
    public Result<Void> saveProductFavorites(String productId) {
        if (isEmptyOrNull(productId)) return fail(Status.BAD_REQUEST, "商品id不允许为空");
        Products products = Db.getById(productId, Products.class);
        if (ObjectUtils.isEmpty(products)) return fail(Status.BAD_REQUEST, "商品不存在");

        String userId = jwtUtil.parseToken(getToken(), Users.class).getId();
        ProductFavorites favorites = new ProductFavorites();
        favorites.setProductId(productId);
        favorites.setUserId(userId);
        boolean save = save(favorites);
        return saveResult(save, "收藏成功", "收藏失败");
    }

    /**
     * 取消收藏商品
     *
     * @param productId 商品id
     * @author 杜伟毅
     * @since 2025/3/18 0018 16:10
     */
    @Override
    public Result<Void> cancelFavorites(String productId) {
        if (isEmptyOrNull(productId)) return fail(Status.BAD_REQUEST, "商品id不允许为空");
        Products products = Db.getById(productId, Products.class);
        if (ObjectUtils.isEmpty(products)) return fail(Status.BAD_REQUEST, "商品不存在");
        String userId = jwtUtil.parseToken(getToken(), Users.class).getId();

        LambdaQueryWrapper<ProductFavorites> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductFavorites::getProductId, productId)
                .eq(ProductFavorites::getUserId, userId);
        boolean remove = remove(lqw);
        return handleResult(remove, "取消收藏成功", "取消收藏失败");
    }

    /**
     * 取消收藏商品(系统)
     *
     * @param productIds 商品id集合
     * @author 杜伟毅
     * @since 2025/3/18 0018 16:18
     */
    @Override
    public void cancelFavoritesSystem(List<String> productIds) {
        if (isListEmpty(productIds)) throw new EmptyIdException("deleteImage");
        LambdaQueryWrapper<ProductFavorites> lqw = new LambdaQueryWrapper<>();
        lqw.in(ProductFavorites::getProductId, productIds);
        remove(lqw);
    }

    //-----------------------------------------------------------------

    /**
     * 接收MQ信息，调用删除商品收藏方法
     *
     * @author 杜伟毅
     * @since 2025/3/26 0026 0:25
     */
    @RabbitListener(queues = FAVORITES_REQUEST_QUEUE)
    public void handleFavoritesRequest(List<String> productIds) {
        cancelFavoritesSystem(productIds);
    }
}
